package com.hankol.message

import androidx.lifecycle.Observer
import com.alibaba.android.arouter.facade.annotation.Route
import com.baselibrary.Constants
import com.baselibrary.activity.BaseActivity
import com.baselibrary.extension.checkNotNull
import com.baselibrary.extension.checkValue
import com.baselibrary.manager.SensorsDataManager
import com.baselibrary.router.ActivityRouter
import com.baselibrary.utils.ARouterUtils
import com.baselibrary.utils.DoubleClickHelper
import com.baselibrary.utils.EmptyViewUtil
import com.hankol.message.adapter.SystemMsgListAdapter
import com.hankol.message.bean.BroadcastMsgBean
import com.hankol.message.databinding.ActivityLikeAndCollectBinding
import com.hankol.message.viewmodel.HomeMessageViewModel

/**广播 列表
 * @author: merbng
 * @time: 2022/3/25 10:15
 * @description:
 */
@Route(path = ActivityRouter.MESSAGE.SYSTEM_MESSAGE_LIST)
class SystemMsgListActivity : BaseActivity() {

    private lateinit var viewModel: HomeMessageViewModel
    private lateinit var binding: ActivityLikeAndCollectBinding
    val adapter: SystemMsgListAdapter by lazy { SystemMsgListAdapter() }
    var msgPushTypeId = 0
    var title = "广播"
    var cursor = "0"
    private var hasnext: Boolean = false
    override fun getLayoutId(): Int {
        return R.layout.activity_like_and_collect
    }

    override fun init() {
        binding = getDataBinding()
        viewModel = getActivityViewModelProvider(this).get(HomeMessageViewModel::class.java)
        if (intent.extras.checkNotNull() && ARouterUtils.parseParams(intent.extras!!, "msgPushTypeId")[0].checkNotNull()) {
            msgPushTypeId = ARouterUtils.parseParams(intent.extras!!, "msgPushTypeId")[0].toInt()
            title = ARouterUtils.parseParams(intent.extras!!, "title")[0].checkValue()
        }
        binding.tvTitle.text = title
        binding.backImageView.setOnClickListener { finish() }
        binding.rvLike.setAdapter(adapter)
        adapter.setOnItemClickListener { adapter, view, position ->
            if (!DoubleClickHelper.isOnDoubleClick()) {
                SensorsDataManager.instance.brookPageElementClick("系统消息详情列表")
                val bean = adapter.data[position] as BroadcastMsgBean
                //进入对应详情页面
                ARouterUtils.jump(bean.routeUrl.checkValue())
            }
        }
        binding.smartRefresh.setOnRefreshListener {
            cursor = "0"
            loadData()
        }
        binding.smartRefresh.setOnLoadMoreListener {
            if (hasnext) {
                loadData()
            } else {
                binding.smartRefresh.finishLoadMore()
            }
        }
        observerCallBck()
        loadData()
        //获取页面开始时间
        SensorsDataManager.instance.onCreateTime()
    }

    private fun observerCallBck() {
        viewModel.broadcastDataListLD.observe(this, Observer {
            hideLoadingDialog()
            binding.smartRefresh.finishRefresh()
            if (it == null || it.data?.size == 0) {
                //无数据
                if (cursor == "0") {
                    adapter.emptyView = EmptyViewUtil.getEmptyView(Constants.TYPE_LIKE_COMMENT, binding.rvLike, "暂无相关信息")
                } else {
                    binding.smartRefresh.finishLoadMore()
                }
            } else {
                if (it.data != null) {
                    hasnext = it.totalPage != adapter.data.size
                    binding.smartRefresh.finishLoadMore()
                    if (cursor == "0") {
                        adapter.setNewData(it.data)
                    } else {
                        adapter.addData(it.data)
                    }
                    cursor= it.nextCursor
                }
            }
        })
    }

    fun loadData() {
        showLoadingDialog()
        viewModel.reqSystemMsgList(msgPushTypeId, cursor)
    }

    override fun onPause() {
        super.onPause()
        //获取页面结束时间
        SensorsDataManager.instance.onDestroyTime()
        if (SensorsDataManager.instance.getBrowseTime() != 0L) {
            SensorsDataManager.instance.brookPageView("系统消息列表页")
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        //获取页面结束时间
        SensorsDataManager.instance.onDestroyTime()
        if (SensorsDataManager.instance.getBrowseTime() != 0L) {
            SensorsDataManager.instance.brookPageView("系统消息列表页")
        }
    }
}